<script setup lang="ts">

import router from "@/router";
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'
import {getInfo} from "@/api/home/login/login";

let headers = ref({'Authorization': localStorage.getItem('token')})
const imageUrl = ref('')
let url = 'http://192.168.5.120:8090'
let user = ref({})

getInfo().then(res=>{
    if (res.code == 401) {
        ElMessage.error('登录认证过期，请重新登录！')
        router.push({path: `/login`})
    }
    if (res.code == 200) {
        user.value = JSON.parse(localStorage.getItem('userInformation'))
        console.log(user.value)
        imageUrl.value = url + user.value.avatar
    }
})

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    imageUrl.value = URL.createObjectURL(uploadFile.raw!)
    console.log(response)
}

// const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
//     if (rawFile.type !== 'image/jpeg') {
//         ElMessage.error('Avatar picture must be JPG format!')
//         return false
//     } else if (rawFile.size / 1024 / 1024 > 2) {
//         ElMessage.error('Avatar picture size can not exceed 2MB!')
//         return false
//     }
//     return true
// }

function revise() {
    // 修改头像

}

</script>

<template>
    <div class="box">
        <div class="titTime" @click="router.go(-1)">
            <van-icon name="arrow-left"/>
            <span class="titleText">修改头像</span>
        </div>
        <div class="substance">
            <el-upload
                class="avatar-uploader"
                action="http://192.168.5.120:8090/system/student/avatar"
                :headers="headers"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
            >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
        </div>
        <div class="uploadBtn">
            <el-button style="width: 50vw" type="primary" @click="revise">修改头像</el-button>
        </div>
    </div>
</template>

<style scoped>
* {
    margin: 0;
    padding: 0;
}

.box {
    padding: 8% 6%;

    .titTime {
        margin-bottom: 5%;
        font-size: 5vw;
    }
}

.substance {
    margin-top: 15%;
    min-height: 25vw;
}

.uploadBtn {
    margin: auto;
    padding: 0 20%;
}

.avatar-uploader {
    width: 100%;
    height: 100%;
    .avatar {
        width: 100%;
    }
}

.avatar-uploader-icon {
    position: relative;
    left: 40vw;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100%;
    height: 100%;
    text-align: center;
}
</style>